// pages/cart/cart.js
const app = getApp()
var host = app.globalData.host;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cart: [],
        selected: false,
        selectedAll: true,
        totalPrice: 0,
        num: 1,
        goodsId: '',
        host: host,


    },
    onLoad() {
        this.loadCart()
    },
    loadCart: function() {
        var that = this;
        var userId = wx.getStorageSync("userId");
        if (userId != "") {
            wx.request({
                url: host + '/api/cart/getShoppingCartList',
                method: 'GET',
                data: {
                    userId,
                    'type': '0'
                },
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    // console.log(res);
                    var carts = res.data.data;
                    // console.log(carts)
                    that.setData({ carts: carts });
                    // console.log(res.header)
                }
            })
        } else {
            wx.redirectTo({
                url: '../login/login'
            })
        }
    },



    addGoods: function(e) {
        // console.log(e)
        var id = e.target.id;
        var carts = this.data.carts;
        for (var i = 0; i < carts.length; i++) {
            var cart = carts[i];
            if (id == cart.id) {
                cart.num = cart.num + 1;
                this.updateCartNum(cart.id, cart.num);
                this.computePrice(id);
                break;
            }
        }
        this.loadCart();

    },
    minusGoods: function(e) {
        var id = e.target.id;
        var carts = this.data.carts;
        for (var i = 0; i < carts.length; i++) {
            var cart = carts[i];
            if (id == cart.id && cart.num > 0) {
                cart.num = cart.num - 1;
                this.updateCartNum(cart.id, cart.num);
                this.computePrice(id);
                break;
            }
        }
        this.loadCart();
    },
    updateCartNum: function(cartId, num) {
        wx.request({
            url: host + '/api/cart/updateCartNum',
            method: 'Get',
            data: {
                cartId: cartId,
                num: num
            },
            header: {
                'Content-Type': 'application/json'
            },
            success: function(res) {

            }
        })
    },
    radioChange: function(e) {
        console.log(e);
        var id = e.detail.value;
        this.computePrice(id);

    },
    checkboxChange: function(e) {
        const items = this.data.items
        const values = e.detail.value
    },
    // 计算总价格
    computePrice: function(id) {
        var carts = this.data.carts;
        var totalPrice = 0;
        for (var i = 0; i < carts.length; i++) {
            var cart = carts[i];
            if (cart.id == id) {
                totalPrice = cart.goodsPrice * cart.num + totalPrice;
                this.setData({ goodsId: cart.goodsId })
                this.setData({ num: cart.num })
                break;
            }
        }
        this.setData({ totalPrice: totalPrice.toFixed(2) });

    },

    buy: function() {
        var goodsId = this.data.goodsId;
        var userId = wx.getStorageSync("userId");
        if (goodsId == '' || goodsId == null) {
            wx.showModal({
                title: '提示',
                content: '请选择结算商品',
                showCancel: true
            })
        } else {
            wx.navigateTo({
                url: '../buy/buy?goodsId=' + goodsId + '&num=' + this.data.num
            })
        }
    }






})